<?php
/**
 * 后勤/财务端登陆接口
 */
class Apiv1_HcloginController extends Ctrl_Api{
	public function indexAction(){
		$p = $_REQUEST;
        $pUsername = empty($p['username'])?'':trim($p['username']);         
        $pPassword = empty($p['password'])?'':trim($p['password']);         
        $tTime = time();

		if(empty($pUsername)){Tool_Fnc::ajaxMsg('请输入账号');}
		if(empty($pPassword)){Tool_Fnc::ajaxMsg('请输入密码');}
        //if(!Tool_Validate::mo($pPhone)){Tool_Fnc::ajaxMsg('账号不正确');}
		$tAMO = new AdminModel;
        $tARow = $tAMO->field('private_key,role')->where('name = \''.$pUsername.'\' and (role = 10 or role = 5)')->fRow();
		$tAkey = empty($tARow['private_key'])?'':$tARow['private_key'];

        $tMd5str = Tool_Fnc::markmd5($pPassword,$tAkey); 
		//$tARow = $tAMO->field('id')->where('name = \''.$pUsername.'\' and password = \''.$tMd5str.'\' and role = 7')->fRow();
        $tSql = 'select id from '.$tAMO->table.' where name = \''.$pUsername.'\' and passwd = \''.$tMd5str.'\' and (role = 10 or role = 5) and status = 1';
        $tRow = $tAMO->query($tSql)[0];
        //var_dump($tRow);exit;
        if(empty($tRow['id'])){Tool_Fnc::ajaxMsg("用户名或者密码错误");}


		//添加登陆 记录
		$tData = array(
			'aid' => $tARow['id'],
			'created' => $tTime,
			'ip' => Tool_Fnc::realip(),
		);
        $tSWLLMO = new SupportworkerloginlogModel;

		if(!$tSWLLMO->insert($tData)){
			Tool_Fnc::ajaxMsg("登录失败");
		}

        $tTokenarr=$tAMO->creattoken($tRow['id']);
        $tTokenarr['role'] = $tARow['role'];
        Tool_Fnc::ajaxMsg("登录成功",1,$tTokenarr);//登录成功返回token和账号信息
	}
}

